/*package com.example.demo.controller;


import com.example.demo.config.AppConfig;
import com.example.demo.dao.UserDao;
import com.example.demo.entity.dto.TokenUserDto;
import com.example.demo.entity.vo.ResponseVO;
import com.example.demo.entity.pojo.User;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.*;

import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/account")
public class AccountController extends BaseController {

    @Autowired
    private JavaMailSender mailSender;

    @Autowired
    private AppConfig appConfig;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private UserDao userDao;


    @PostMapping("/sendPwd")
    public ResponseVO sendPwd(String email){
        System.out.println("实际发送的 email：" + email);
        // 确保邮箱地址没有额外引号或HTML实体
        String cleanEmail = email.replace("&quot;", "").replace("\"", "");
        rabbitTemplate.convertAndSend("ex","email", cleanEmail);
        return getSuccess("成功发送验证码！",null);
    }


    @PostMapping("/loginByEmail")
    public ResponseVO<User> loginByEmail(@NotNull String email, String code){
        if(!redisTemplate.opsForValue().get("your_email_code:"+code).equals(code))
            return getFail("验证码错误");
        User user = userDao.queryByEmail(email);
        User newuser = new User();
        String token = RandomStringUtils.random(21, true, true);
        if(user==null){
            newuser.setEmail(email);
            newuser.setUserId("U"+RandomStringUtils.random(10,false,true));
            newuser.setUsername("新用户"+RandomStringUtils.random(6,true,true));
            userDao.insert(newuser);
            newuser.setToken(token);
            redisTemplate.opsForValue().set("token:"+token,new TokenUserDto(email, newuser.getUserId(), newuser.getUsername()));
            return getSuccess("登陆成功！",newuser);
        }else {
            user.setToken(token);
            redisTemplate.opsForValue().set("token:"+token,new TokenUserDto(email, user.getUserId(), user.getUsername()));
            return getSuccess("登陆成功！",user);
        }
    }



    @PostMapping("/chooseRole")
    public ResponseVO<String> chooseRole(HttpServletRequest request,String role){
        TokenUserDto tokenUserDto = getTokenUserDto(request);
        User user = new User();
        user.setRole(role);
        user.setUserId(tokenUserDto.getUserId());
        userDao.update(user);
        return getSuccess("角色选择成功！",role);


    }

    @PostMapping("/loginByPwd")
    public ResponseVO<User> loginByPwd(@NotNull String email,@NotNull String password){
        User user = userDao.queryByEmail(email);
        if(user==null || !user.getPassword().equals(DigestUtils.md5Hex(password)))
            return getFail("账号或密码错误！");
        String token = RandomStringUtils.random(21, true, true);
        user.setToken(token);
        redisTemplate.opsForValue().set("token:"+token,new TokenUserDto(email, user.getUserId(), user.getUsername()));
        return getSuccess("登陆成功！",user);
    }


    @PostMapping("/forgetPwd")
    public  ResponseVO forgetPwd(String email,String code,String newPwd ){
        if(!redisTemplate.opsForValue().get("your_email_code:"+code).equals(code))
            return getFail("验证码错误");
        User user = userDao.queryByEmail(email);
        user.setPassword(DigestUtils.md5Hex(newPwd));
        userDao.update(user);
        return getSuccess("密码修改成功！",null);
    }

    @RabbitListener(queues = "que")
    private void sendEmail(Message message){
        // 修改消息体获取方式，添加字符集参数
        String email = new String(message.getBody(), StandardCharsets.UTF_8);
        // 清理邮箱地址中可能存在的引号和HTML实体
        email = email.replace("&quot;", "").replace("\"", "");
        System.out.println("解码后收件人：" + email);
        String code = RandomStringUtils.random(6,false,true);
        SimpleMailMessage message1 = new SimpleMailMessage();
        message1.setFrom(appConfig.getMyEmail()); // 与配置一致
        message1.setTo(email);
        message1.setSubject("登录验证码");
        message1.setText("你的登录验证码是：" + code + "，有效期 5 分钟！");
        redisTemplate.opsForValue().set("your_email_code:"+code,code,5, TimeUnit.MINUTES);
        mailSender.send(message1);
    }


}*/
